package com.cn.yunhe.rentcars.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.UserMapper;
import com.cn.yunhe.rentcars.model.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
public class AdminController {
	
	@Autowired
	private UserMapper userMapper;
	
	@RequestMapping(value="/cspage", method=RequestMethod.GET)
	public ModelAndView cspage() {	
		return new ModelAndView("cs");
	}
	
	@RequestMapping(value="/admin-list", method=RequestMethod.GET)
	public ModelAndView adminList() {	
		return new ModelAndView("admin-list");
	}

	@RequestMapping(value="/admin-add", method=RequestMethod.GET)
	public ModelAndView showAddAdmin() {	
		return new ModelAndView("admin-add");
	}
	
	@RequestMapping(value="/admin-edit", method=RequestMethod.GET)
	public ModelAndView showEditAdmin(@RequestParam String userid) {	
		
		User user = userMapper.getUserById(userid);
		
		Map<String,User> modelMap = new HashMap<String,User>();
		modelMap.put("user", user);
		
		return new ModelAndView("admin-edit",modelMap);		
	}
	
	@RequestMapping(value="/getadminbyname", method=RequestMethod.GET)
	public PageInfo<User> getAdminListByName(@RequestParam String username,@RequestParam  int pageNum,@RequestParam  int pageSize){
	      PageHelper.startPage(pageNum, pageSize);
	      List<User> userList = userMapper.getUsers(username);
	      PageInfo<User> page = new PageInfo<User>(userList);
	      return page;
	}
	
	
	/**
	 * 添加新用户
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/addUser", method=RequestMethod.POST)
	public void addUser(@RequestBody User user){
		user.setId(UUID.randomUUID().toString());
		userMapper.addUser(user);
	}
	
	/**
	 * 修改用户
	 * 
	 * @param userid
	 */
	@RequestMapping(value="/users/{userid}", method=RequestMethod.PUT)
	public void editUser(@PathVariable("userid") String userid,@RequestBody User user){
		user.setId(userid);
		userMapper.editUser(user);
	}
	

	

	/**
	 * 删除用户信息
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/delUser", method=RequestMethod.POST)
	public void delUser(@RequestParam String userid){ 
		userMapper.delUser(userid);
	}
	
	/**
	 * 删除用户信息
	 * 
	 * @param userid
	 * @param isabled
	 */
	@RequestMapping(value="/delUsers", method=RequestMethod.POST)
	public void delUsers(@RequestParam String usersid){ 
		List<String> usersidList = Arrays.asList(usersid.split(","));  		
		userMapper.delUsers(usersidList);
	}
	
	
	/**
	 * 搜索查询用户列表信息
	 * 
	 * @param username
	 * @param draw 
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value="/users", method=RequestMethod.GET)
	public Map<String,Object> getLikeUsers(@RequestParam String datemin,@RequestParam String datemax,@RequestParam String username,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		  
		  Map<String,String> params =  new HashMap<String,String>();
		  params.put("username", username);
		  params.put("datemin", datemin);
		  params.put("datemax", datemax);
		
		  int pageNum = (start/length)+1;
		  int pageSize = length;		 
		  
	      PageHelper.startPage(pageNum, pageSize);
	      List<User> userList = userMapper.searchUsers(params);
	      PageInfo<User> page = new PageInfo<User>(userList);
	      
	      Map<String,Object> rsMap = new HashMap<String,Object>();
	      rsMap.put("draw",draw);
	      rsMap.put("data", page.getList());	      
	      rsMap.put("recordsTotal", page.getTotal());
	      rsMap.put("recordsFiltered", page.getTotal());
	      return rsMap;
	}
}
